/*     */ package com.zimbra.cs.filter;
/*     */ 
/*     */ import com.google.common.collect.Sets;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.filter.jsieve.ActionFlag;
/*     */ import com.zimbra.cs.mailbox.DeliveryContext;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mailbox.Mountpoint;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mime.ParsedMessage;
/*     */ import com.zimbra.cs.mime.ParsedMessageOptions;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import java.util.Collection;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class ExistingMessageHandler
/*     */   implements FilterHandler
/*     */ {
/*     */   private final OperationContext octxt;
/*     */   private final Mailbox mailbox;
/*     */   private final int messageId;
/*     */   private final int size;
/*     */   private Message message;
/*     */   private MimeMessage mimeMessage;
/*     */   private ParsedMessage parsedMessage;
/*  54 */   private boolean kept = false;
/*  55 */   private boolean filed = false;
/*  56 */   private boolean filtered = false;
/*     */   
/*     */   public ExistingMessageHandler(OperationContext octxt, Mailbox mbox, int messageId, int size) {
/*  59 */     this.octxt = octxt;
/*  60 */     this.mailbox = mbox;
/*  61 */     this.messageId = messageId;
/*  62 */     this.size = size;
/*     */   }
/*     */   
/*     */   public String getDefaultFolderPath() throws ServiceException
/*     */   {
/*  67 */     return getDefaultFolder().getPath();
/*     */   }
/*     */   
/*     */   private Folder getDefaultFolder() throws ServiceException {
/*  71 */     return this.mailbox.getFolderById(this.octxt, 2);
/*     */   }
/*     */   
/*     */   public Message getMessage() throws ServiceException
/*     */   {
/*  76 */     if (this.message == null) {
/*  77 */       this.message = this.mailbox.getMessageById(this.octxt, this.messageId);
/*     */     }
/*  79 */     return this.message;
/*     */   }
/*     */   
/*     */   public MimeMessage getMimeMessage() throws ServiceException
/*     */   {
/*  84 */     if (this.mimeMessage == null) {
/*  85 */       this.mimeMessage = getMessage().getMimeMessage();
/*     */     }
/*  87 */     return this.mimeMessage;
/*     */   }
/*     */   
/*     */   public ParsedMessage getParsedMessage() throws ServiceException
/*     */   {
/*  92 */     if (this.parsedMessage == null) {
/*  93 */       Message msg = getMessage();
/*  94 */       ParsedMessageOptions opt = new ParsedMessageOptions().setContent(msg.getMimeMessage()).setAttachmentIndexing(this.mailbox.attachmentsIndexingEnabled()).setSize(msg.getSize()).setDigest(msg.getDigest());
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*  99 */       this.parsedMessage = new ParsedMessage(opt);
/*     */     }
/* 101 */     return this.parsedMessage;
/*     */   }
/*     */   
/*     */   public boolean filtered() {
/* 105 */     return this.filtered;
/*     */   }
/*     */   
/*     */   public void discard() throws ServiceException
/*     */   {
/* 110 */     ZimbraLog.filter.info("Discarding existing message with id %d.", new Object[] { Integer.valueOf(this.messageId) });
/* 111 */     this.mailbox.delete(this.octxt, this.messageId, MailItem.Type.MESSAGE);
/* 112 */     this.filtered = true;
/*     */   }
/*     */   
/*     */   public Message implicitKeep(Collection<ActionFlag> flagActions, String[] tags)
/*     */     throws ServiceException
/*     */   {
/* 118 */     ZimbraLog.filter.debug("Implicitly keeping existing message %d.", new Object[] { Integer.valueOf(this.messageId) });
/* 119 */     Message msg = getMessage();
/* 120 */     updateTagsAndFlagsIfNecessary(msg, flagActions, tags);
/* 121 */     this.kept = true;
/* 122 */     return msg;
/*     */   }
/*     */   
/*     */   public Message explicitKeep(Collection<ActionFlag> flagActions, String[] tags) throws ServiceException
/*     */   {
/* 127 */     ZimbraLog.filter.debug("Explicitly keeping existing message %d.", new Object[] { Integer.valueOf(this.messageId) });
/* 128 */     Message msg = getMessage();
/* 129 */     updateTagsAndFlagsIfNecessary(msg, flagActions, tags);
/* 130 */     this.kept = true;
/* 131 */     return msg;
/*     */   }
/*     */   
/*     */   private void updateTagsAndFlagsIfNecessary(Message msg, Collection<ActionFlag> flagActions, String[] newTags) throws ServiceException
/*     */   {
/* 136 */     String[] existingTags = msg.getTags();
/* 137 */     String[] tags = FilterUtil.getTagsUnion(existingTags, newTags);
/* 138 */     int flags = FilterUtil.getFlagBitmask(flagActions, msg.getFlagBitmask());
/* 139 */     if ((!Sets.newHashSet(existingTags).equals(Sets.newHashSet(tags))) || (msg.getFlagBitmask() != flags)) {
/* 140 */       ZimbraLog.filter.info("Updating flags to %d, tags to %s on message %d.", new Object[] { Integer.valueOf(flags), tags, Integer.valueOf(msg.getId()) });
/* 141 */       this.mailbox.setTags(this.octxt, msg.getId(), MailItem.Type.MESSAGE, flags, tags);
/* 142 */       this.filtered = true;
/*     */     }
/*     */   }
/*     */   
/*     */   public ItemId fileInto(String folderPath, Collection<ActionFlag> flagActions, String[] tags)
/*     */     throws ServiceException
/*     */   {
/* 149 */     Message source = getMessage();
/*     */     
/*     */ 
/* 152 */     Folder targetFolder = null;
/*     */     try {
/* 154 */       targetFolder = this.mailbox.getFolderByPath(this.octxt, folderPath);
/*     */     }
/*     */     catch (MailServiceException.NoSuchItemException ignored) {}
/* 157 */     if ((targetFolder != null) && (source.getFolderId() == targetFolder.getId())) {
/* 158 */       ZimbraLog.filter.debug("Ignoring fileinto action for message %d.  It is already in %s.", new Object[] { Integer.valueOf(this.messageId), folderPath });
/*     */       
/* 160 */       updateTagsAndFlagsIfNecessary(source, flagActions, tags);
/* 161 */       return null;
/*     */     }
/*     */     
/* 164 */     ZimbraLog.filter.info("Copying existing message %d to folder %s.", new Object[] { Integer.valueOf(this.messageId), folderPath });
/* 165 */     if (isLocalExistingFolder(folderPath))
/*     */     {
/* 167 */       Folder target = this.mailbox.getFolderByPath(this.octxt, folderPath);
/* 168 */       Message newMsg = (Message)this.mailbox.copy(this.octxt, this.messageId, MailItem.Type.MESSAGE, target.getId());
/* 169 */       this.filtered = true;
/* 170 */       this.filed = true;
/*     */       
/*     */ 
/* 173 */       this.mailbox.setTags(this.octxt, newMsg.getId(), MailItem.Type.MESSAGE, FilterUtil.getFlagBitmask(flagActions, source.getFlagBitmask()), FilterUtil.getTagsUnion(source.getTags(), tags));
/*     */       
/*     */ 
/* 176 */       return new ItemId(this.mailbox, newMsg.getId());
/*     */     }
/*     */     
/* 179 */     ItemId id = FilterUtil.addMessage(new DeliveryContext(), this.mailbox, getParsedMessage(), this.mailbox.getAccount().getName(), folderPath, false, FilterUtil.getFlagBitmask(flagActions, source.getFlagBitmask()), tags, -1, this.octxt);
/*     */     
/*     */ 
/*     */ 
/* 183 */     if (id != null) {
/* 184 */       this.filtered = true;
/* 185 */       this.filed = true;
/*     */     }
/* 187 */     return id;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private boolean isLocalExistingFolder(String folderPath)
/*     */     throws ServiceException
/*     */   {
/* 195 */     Pair<Folder, String> folderAndPath = this.mailbox.getFolderByPathLongestMatch(this.octxt, 1, folderPath);
/*     */     
/* 197 */     Folder folder = (Folder)folderAndPath.getFirst();
/* 198 */     String remainingPath = (String)folderAndPath.getSecond();
/* 199 */     if (((folder instanceof Mountpoint)) || (!StringUtil.isNullOrEmpty(remainingPath))) {
/* 200 */       return false;
/*     */     }
/* 202 */     return true;
/*     */   }
/*     */   
/*     */   public void redirect(String destinationAddress)
/*     */   {
/* 207 */     ZimbraLog.filter.debug("Ignoring attempt to redirect existing message %d to %s.", new Object[] { Integer.valueOf(this.messageId), destinationAddress });
/*     */   }
/*     */   
/*     */ 
/*     */   public void reply(String bodyTemplate)
/*     */   {
/* 213 */     ZimbraLog.filter.debug("Ignoring attempt to reply to existing message %d", new Object[] { Integer.valueOf(this.messageId) });
/*     */   }
/*     */   
/*     */ 
/*     */   public void notify(String emailAddr, String subjectTemplate, String bodyTemplate, int maxBodyBytes, List<String> origHeaders)
/*     */   {
/* 219 */     ZimbraLog.filter.debug("Ignoring attempt to notify for existing message %d", new Object[] { Integer.valueOf(this.messageId) });
/*     */   }
/*     */   
/*     */ 
/*     */   public void beforeFiltering() {}
/*     */   
/*     */   public void afterFiltering()
/*     */     throws ServiceException
/*     */   {
/* 228 */     if ((this.filed) && (!this.kept)) {
/* 229 */       ZimbraLog.filter.info("Deleting original message %d after filing to another folder.", new Object[] { Integer.valueOf(this.messageId) });
/* 230 */       this.mailbox.delete(this.octxt, this.messageId, MailItem.Type.MESSAGE);
/*     */     }
/*     */   }
/*     */   
/*     */   public int getMessageSize()
/*     */   {
/* 236 */     return this.size;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/filter/ExistingMessageHandler.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */